﻿


/**
 * 注册快捷建
 * */
window.shortcuts = {
    add: function (keyword, namespace, methodname) {

        console.log("add:", keyword, namespace, methodname)  

        $.Shortcuts.add({
            type: 'down',
            mask: keyword, // 'Ctrl+s'
            handler: function () {
                console.log('handel ' + keyword)
                DotNet.invokeMethod(namespace, methodname);
            }
        });
    },

    start: function () {
        console.log("start")  
        $.Shortcuts.start();
    }
};

window.editorSaveEvent = function () {
    console.log('editorSaveEvent')
    DotNet.invokeMethod('MyDocuments.Client', 'SaveEvent');
}



function openBrowserUrl(url, id) {
    let a = document.createElement("a");
    a.setAttribute("href", url);
    a.setAttribute("target", "_blank");
    a.setAttribute("id", id);
    // 防止反复添加      
    if (!document.getElementById(id)) {
        document.body.appendChild(a);
    }
    a.click();
}

// markdown
window.markdowFormat.markedutils.setOptions(async function (code, language) {
    if (language == "mermaid") {
        try {
            var result = await window.markdowFormat.mRenderAsync("mermaid", code)
            return result.svg;
        } catch (e) {
            return code;
        }
    } else {
        let hljs = window.markdowFormat.markedutils.highlightJS;
        const validLanguage = hljs.getLanguage(language) ? language : 'javascript';
        return hljs.highlight(validLanguage, code).value
    }
});

function jump(id) {
    document.querySelector('#' + id).scrollIntoView({ behavior: "smooth" })
}

async function parsemarkdown(id, plainText) {
    document.getElementById(id).innerHTML = await window.markdowFormat.markedutils.parse(plainText);

    if (id == "MarkdownHtmlPriview") {
        return;
    }

    // 目录部分
    setTimeout(function () {

        let tags = [];

        $("h2,h3,h4,h5,h6").each(function (i, item) {
            var tag = $(item).get(0).localName;
            $(item).attr("id", "wow" + i);
            $("#category").append('<li class="catalog-li catalog-wow' + i + '"><a class="new' + tag + '" onclick="jump(\'wow' + i + '\')">' + $(this).text() + '</a></li>');
            tags.push(tag[1]-0)
        });

        // 排序并且去除重复值
        tags = [...new Set([...tags].sort((a, b) => a-b))]

        for (var i = 0; i < tags.length; i++) {
            $(".newh" + tags[i]).css("margin-left", i * 22);
        }

        //$(".newh2").css("margin-left", 0);
        //$(".newh3").css("margin-left", 20);
        //$(".newh4").css("margin-left", 40);
        //$(".newh5").css("margin-left", 60);
        //$(".newh6").css("margin-left", 80);

        var $sections = $('#MarkdownHtmlC :header');

        $(document).scroll(function () {

            var top = $(document).scrollTop()

            if (top > 140) {
                $("body").addClass("sticky")
            } else {
                $("body").removeClass("sticky")
            }

            let $currentSection = null;
            $sections.each(function () {
                var divPosition = $(this).offset().top;
                if (divPosition - 1 < top) {
                    $currentSection = $(this);
                }

                if (top > 300 && $currentSection != null) {
                    var id = $currentSection.attr('id');
                    $('.catalog-li').removeClass('menu-active');
                    $('.catalog-' + id).addClass('menu-active');
                }
            });
        });
    }, 666);
}



